# -*- coding: utf-8 -*-
import requests
import json

# PowerStore API 相关信息
USERNAME = "monitor"
PASSWORD = "OPS@2528sjzx"
BASE_URL = "https://10.2.158.156/api/rest"

# 关闭 SSL 证书验证警告
requests.packages.urllib3.disable_warnings()


def get_auth_token():
    """获取 PowerStore API 认证 Token"""
    url = "{}/login_session".format(BASE_URL)
    response = requests.post(url, auth=(USERNAME, PASSWORD), verify=False)
    response.raise_for_status()
    return response.cookies


def get_capacity(cookies):
    """获取存储容量信息"""
    url = "{}/storage_container".format(BASE_URL)
    response = requests.get(url, cookies=cookies, verify=False)
    response.raise_for_status()
    return response.json()


def get_performance_metrics(cookies):
    """获取性能指标 (IOPS、带宽、延迟)"""
    url = "{}/metrics/generate".format(BASE_URL)
    payload = {"entity": "performance_metrics_by_appliance", "interval": "Five_Min"}
    response = requests.post(url, json=payload, cookies=cookies, verify=False)
    response.raise_for_status()
    return response.json()


def get_alerts(cookies):
    """获取当前警报信息"""
    url = "{}/alert".format(BASE_URL)
    response = requests.get(url, cookies=cookies, verify=False)
    response.raise_for_status()
    return response.json()


def main():
    try:
        cookies = get_auth_token()

        print("获取存储容量信息...")
        capacity_info = get_capacity(cookies)
        print(json.dumps(capacity_info, indent=2))

        print("获取性能监控数据...")
        performance_metrics = get_performance_metrics(cookies)
        print(json.dumps(performance_metrics, indent=2))

        print("获取系统警报信息...")
        alerts = get_alerts(cookies)
        print(json.dumps(alerts, indent=2))

    except requests.exceptions.RequestException as e:
        print("API 请求错误: {}".format(e))
    finally:
        print("监控任务完成！")


if __name__ == "__main__":
    main()